package com.spring.sunflower.dialog

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.lxj.xpopup.core.CenterPopupView
import com.spring.sunflower.R
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.util.LogUtils

import com.blankj.utilcode.util.AppUtils
import com.lxj.xpopup.util.XPopupUtils
import com.spring.sunflower.common.WebViewActivity


/**
 * Created by Kevin on 2021/8/6<br/>
 *
 * Describe:<br/>
 */
class PersonInfoIntroDialog(
    context: Context,
    var layoutID: Int,
    var listener: OnPermissionIntroClickListener?,
    var showHyper: Boolean
) : CenterPopupView(context) {
    private var llContainer: LinearLayout? = null
    override fun getImplLayoutId(): Int {
        return layoutID
    }

    override fun onCreate() {
        super.onCreate()
        val tvOK: TextView = findViewById(R.id.tvAgree)
        val tvCancel: TextView = findViewById(R.id.tvCancel)
        val tvContent: TextView = findViewById(R.id.tvContent)

        xListener?.onCreateView()
        tvOK.setOnClickListener {
            listener?.onClickOK()
            dismiss()
        }
        tvCancel.setOnClickListener {
            listener?.onClickCancel()
            dismiss()
        }
        if (showHyper) {
            val appName = AppUtils.getAppName()
            val linkWord1 = Constant.WELCOME_CONTENT_2
            val linkWord2 = Constant.WELCOME_CONTENT_4
            val content = "欢迎使用$appName！\n\n" + context.getString(R.string.welcome_dialog_content_new)
            val spannableString = SpannableStringBuilder(toDBC(content))
            val index1 = content.indexOf(linkWord1)
            val index2 = content.indexOf(linkWord2)
            LogUtils.logD("Welcome", "$content")
            LogUtils.logD("Welcome", "$index1,$index2")
            spannableString.setSpan(object : ClickableSpan() {
                override fun onClick(widget: View) {
                    //用户协议
                    val intent = Intent(context, WebViewActivity::class.java)
                    intent.putExtra(Constant.KEY_WEB_VIEW_TITLE, "用户协议")
                    intent.putExtra(Constant.KEY_WEB_VIEW_URL, Constant.REGISTER_AGREEMENT)
                    context.startActivity(intent)
//                    val intent = Intent()
//                    intent.action = Intent.ACTION_VIEW
//                    val contentUrl: Uri = Uri.parse(Constant.REGISTER_AGREEMENT)
//                    intent.data = contentUrl
//                    context.startActivity(intent)
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = ContextCompat.getColor(context, R.color.orange_3)
                    ds.isUnderlineText = false
                }

            }, index1, index1 + linkWord1.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
            spannableString.setSpan(object : ClickableSpan() {
                override fun onClick(widget: View) {
                    val intent = Intent(context, WebViewActivity::class.java)
                    intent.putExtra(Constant.KEY_WEB_VIEW_TITLE, "隐私协议")
                    intent.putExtra(Constant.KEY_WEB_VIEW_URL, Constant.PRIVATE_AGREEMENT)
                    context.startActivity(intent)
//                    val intent = Intent()
//                    intent.action = Intent.ACTION_VIEW
//                    val contentUrl: Uri = Uri.parse(Constant.PRIVATE_AGREEMENT)
//                    intent.data = contentUrl
//                    context.startActivity(intent)
                }

                override fun updateDrawState(ds: TextPaint) {
                    super.updateDrawState(ds)
                    ds.color = ContextCompat.getColor(context, R.color.orange_3)
                    ds.isUnderlineText = false
                }

            }, index2, index2 + linkWord2.length, Spannable.SPAN_EXCLUSIVE_INCLUSIVE)
            tvContent.text = spannableString
//            tvContent.setTextColor(ContextCompat.getColor(context, R.color.gray_4))
//            tvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16f)
            tvContent.movementMethod = LinkMovementMethod.getInstance()
        }
    }

    fun getContainer(): LinearLayout {
        return llContainer!!
    }

    private var xListener: OnCreateViewListener? = null
    fun setOnCreateViewListener(l: OnCreateViewListener) {
        xListener = l
    }

    fun interface OnCreateViewListener {
        fun onCreateView()
    }

    fun toDBC(input: String): String? {
        val c = input.toCharArray()
        for (i in c.indices) {
            if (c[i].equals(12288)) {
                c[i] = 32.toChar()
                continue
            }
            if (c[i] > 65280.toChar() && c[i] < 65375.toChar()) c[i] = (c[i] - 65248)
        }
        return String(c)
    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getScreenHeight(context) * 0.6f).toInt()
    }
}